package com.course.server.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.course.server.dto.PageDto;
import com.course.server.dto.ResourceDto;
import com.course.server.entity.Resource;
import com.course.server.entity.ResourceExample;
import com.course.server.mapper.ResourceMapper;
import com.course.server.util.CopyUtil;
import com.course.server.util.UuidUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author: Durian
 * @date: 2021/6/6 12:22
 */
@Service
public class ResourceService {

    @javax.annotation.Resource
    private ResourceMapper resourceMapper;

    /**
     * 查询资源，转化为树结构 (Ztree)
     *
     * @return 资源树
     */
    public List<ResourceDto> getResourceTree() {
        ResourceExample example = new ResourceExample();
        example.setOrderByClause("id asc");
        List<Resource> resourceList = resourceMapper.selectByExample(example);
        List<ResourceDto> resourceDtoList = CopyUtil.copyList(resourceList, ResourceDto.class);
        for (int i = resourceDtoList.size() - 1; i >= 0; i--) {
            // 当前要移动的记录
            ResourceDto child = resourceDtoList.get(i);

            // 如果当前节点没有父节点，则不用往下了
            if (StringUtils.isEmpty(child.getParent())) {
                continue;
            }
            // 查找父节点
            for (int j = i - 1; j >= 0; j--) {
                ResourceDto parent = resourceDtoList.get(j);
                if (child.getParent().equals(parent.getId())) {
                    if (CollectionUtils.isEmpty(parent.getChildren())) {
                        parent.setChildren(new ArrayList<>());
                    }
                    // 添加到最前面，否则会变成倒序，因为循环是从后往前循环的
                    parent.getChildren().add(0, child);

                    // 子节点找到父节点后，删除列表中的子节点
                    resourceDtoList.remove(child);
                }
            }
        }
        return resourceDtoList;

        /*ResourceExample example = new ResourceExample();
        // 按id排序 ， 遍历时倒序遍历，按顺序初始化资源树后，减少遍历次数
        example.setOrderByClause("id asc");
        List<Resource> resourceList = resourceMapper.selectByExample(example);
        List<ResourceDto> resourceDtoList = CopyUtil.copyList(resourceList, ResourceDto.class);
        for (int i = 0; i < resourceDtoList.size(); i++) {
            ResourceDto root = resourceDtoList.get(i);
            for (int j = i + 1; j < resourceDtoList.size(); j++) {
                if (Objects.equals(resourceDtoList.get(j).getParent(), root.getId())) {
                    if (CollectionUtil.isEmpty(root.getChildren())) {
                        root.setChildren(new ArrayList<>());
                    }
                    root.getChildren().add(resourceDtoList.get(j));
                }
            }
        }
        return resourceDtoList;*/
    }

    public PageDto<ResourceDto> list(int page, int size) {
        PageHelper.startPage(page, size);
        ResourceExample example = new ResourceExample();
        List<Resource> resources = resourceMapper.selectByExample(example);
        PageInfo<Resource> pageInfo = new PageInfo<>(resources);

        List<ResourceDto> resourceDtos = CopyUtil.copyList(resources, ResourceDto.class);
        PageDto<ResourceDto> pageDto = new PageDto<>();
        pageDto.setPage(page);
        pageDto.setSize(size);
        pageDto.setList(resourceDtos);
        pageDto.setTotal(pageInfo.getTotal());
        return pageDto;
    }

    public void save(ResourceDto resourceDto) {
        Resource resource = new Resource();
        BeanUtils.copyProperties(resourceDto, resource);
        if (StrUtil.isBlank(resourceDto.getId())) {
            insert(resource);
        } else {
            update(resource);
        }
    }

    public void delete(String id) {
        resourceMapper.deleteByPrimaryKey(id);
    }

    private void insert(Resource resource) {
        resource.setId(UuidUtils.getShortUuid());
        resourceMapper.insert(resource);
    }

    private void update(Resource resource) {
        resourceMapper.updateByPrimaryKeySelective(resource);
    }

}
